layui.use(['jquery','layer','table','form'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var btn = 0;
    var user = JSON.parse(localStorage.getItem('user'));
    var userId = user.data.userId;
    getTable('','','','');
    //高级检索
    $('#advancedSearching').on('click',function () {
        layer.open({
            type: 2,
            title: '高级检索',
            content: ['advanced-search.html','no'],
            area: ['690px', '460px'],
            offset: 'auto',
            shade: 0.4,
            resize: false
        })
    });
    //筛选搜索
    $('#search').on('click',function () {
        var productName = $("input[name=productName]").val();
        var searchKeywords = $("input[name=searchKeywords]").val();
        var currentpriceProduct = $("input[name=currentpriceProduct]").val();
        getTable(productName,searchKeywords,currentpriceProduct);
    })
    //高级检索获取数据
    advancedSearch = function advancedSearch(data){
        dataTable(data);
    }
    //获取表格数据
    function getTable(productName,searchKeywords,currentpriceProduct) {
        $.ajax({
            url:'/product/getPersonalProduct',
            type:'post',
            dataType:'json',
            data:{'productName':productName,'searchKeywords':searchKeywords,'currentpriceProduct':currentpriceProduct,'createId':userId,'type':3},
            success:function (data) {
                dataTable(data.data);
            }
        })
    }
    //表格渲染
    function dataTable(data) {
        table.render({
            elem:'#demoTable',
            title:"商品审核列表",
            data: data,
            toolbar:'#tableHeader',
            limit:10,
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                limits:[10,20,30],
                first:'首页',
                last:'尾页',
            },
            cols:[[
                {type:'checkbox'},
                {field:"productCode",title:"商品编号",align:'center'},
                {field:"mainImage",title:"商品图片",align:'center',templet:function(data){
                        return '<img src="'+data.mainImage+'"';
                    }},
                {field:"productName",title:"商品名称",align:'center'},
                {field:"title",title:"商品分类",align:'center'},
                {field:"productLabel",title:"商品标签",align:'center'},
                {field:"searchKeywords",title:"关键字",align:'center'},
                {field:"currentpriceProduct",title:"现价",align:'center'},
                {field:"result",title:"审核状态",align:'center',templet:function(data){
                        if(data.result==1){
                            return '审核通过';
                        }else if(data.result==2){
                            return '待审核';
                        }else{
                            return '审核驳回';
                        }
                        return '<p>'+data.result+'</p><p style="color: #00B83F">审核详情</p>';
                    }},
                {field:"productSate",title:"上架/下架",align:'center',templet:function(data){
                        if (data.productSate == 1) {
                            return '<input type=\"checkbox\" value="' + data.productId + '" lay-skin=\"switch\" lay-text=\"上架|下架"\ checked lay-filter="switchState">';
                        } else if (data.productSate == 2) {
                            return '<input type=\"checkbox\" value="' + data.productId + '" lay-skin=\"switch\" lay-text=\"上架|下架" lay-filter="switchState"\>';
                        } else if (data.productSate == 3) {
                            return '<p>已删除</p>';
                        }
                    }},
                {field:"hits",title:"点击量",align:'center'},
                {title:"操作",align:'center',toolbar:'#table_toolbar'}
            ]],
        })
    }
    //上架/下架商品
    form.on('switch(switchState)', function(data){
        if(data.elem.checked==true){
            $.ajax({
                url:'/product/updateProductState',
                type:'post',
                dataType:'json',
                data:{'productSate':1,'productId':data.value,'updateId':userId},
                success:function (data) {
                    if(data.data){
                        getTable('','','','');
                        layer.msg("修改成功");
                    }
                }
            })
        }else{
            $.ajax({
                url:'/product/updateProductState',
                type:'post',
                dataType:'json',
                data:{'productSate':2,'productId':data.value,'updateId':userId},
                success:function (data) {
                    if(data.data){
                        getTable('','','','');
                        layer.msg("修改成功");
                    }
                }
            })
        }
    })
    //表内操作
    table.on('tool(demoTable)',function (obj) {
        var event = obj.event;
        var productId = obj.data.productId;
        if(event == 'checkTable'){//查看商品详情
            var param = {
                'tableName':'js_product',
                'type':5,
                'userId':userId,
                'remark':'查看反馈统计',
                'logType':1,
            };
            $.ajax({
                url:"/product/addProductLog",
                type:'post',
                dataType:'json',
                data:param,
                success:function (data) {
                    checkTable(productId);
                }
            });
        }else if(event == 'deleteTable'){//删除商品
            layer.confirm('确认要删除吗?', {icon: 3, title:'提示'}, function(index){
                var productId = obj.data.productId;
                var param = {
                    'tableName':'js_product',
                    'type':3,
                    'userId':userId,
                    'remark':'删除反馈统计',
                    'logType':1,
                };
                //添加操作日志
                $.ajax({
                    url:"/product/addProductLog",
                    type:'post',
                    dataType:'json',
                    data:param,
                    success:function (data) {
                    }
                });
                //删除商品
                $.ajax({
                    url:"/product/deleteProduct",
                    type:'post',
                    dataType:'json',
                    data:{'id':productId},
                    success:function (data) {
                    }
                });
                //删除商品审核
                $.ajax({
                    url:"/product/deleteProductRecord",
                    type:'post',
                    dataType:'json',
                    data:{'id':productId,'type':3},
                    success:function (data) {
                    }
                });
                //删除商品评论
                $.ajax({
                    url:"/product/deleteProductComment",
                    type:'post',
                    dataType:'json',
                    data:{'id':productId,'type':3},
                    success:function (data) {
                    }
                });
                layer.close(index);
                getTable('','','','');
            });
        }
    })
    //查看商品详情
    checkTable = function checkTable(id) {
        $.ajax({
            url: '/product/getProductInformation',
            type: 'post',
            dataType: 'json',
            data: {'type':3,'id':id},
            success: function (data) {
                localStorage.setItem('product',JSON.stringify(data.data));
                layer.open({
                    type:2,
                    title:"商品详情",
                    area:['95%','100%'],
                    Boolean:true,
                    maxmin:true,
                    closeBtn:1,
                    btn:["确定","取消"],
                    offset:['0px','35px'],
                    content:"product-details.html",
                    success: function (layero,index) {
                        layero.find('.layui-layer-max').remove();
                        layero.find('.layui-layer-min').remove();
                        var child = layero.find('iframe').contents();
                    }
                })
            }
        })
    };
    //刷新
    $('#refresh').click(function () {
        location.reload();
    })
    //返回
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    $(document).click(function () {
        parent.$('.rightMenu').hide();
    });
})